﻿using System.Data;

namespace SOEI.Solution.Helper
{
    public static class ExtendMethods
    {
        /// <summary>对泛型实体进行初始化
        /// </summary>
        public static T Val<T>(this T pInput) where T : class, new()
        {
            if (pInput == null)
            {
                return new T();
            }

            return pInput;
        }

        /// <summary>对引用属性进行赋值操作
        /// </summary>
        public static T PropVal<T>(this T pInput, string pPropName, object pVal) where T : class, new()
        {
            pInput = pInput.Val();

            var vType = typeof(T);

            var vPropInfo = vType.GetProperty(pPropName);

            if (vPropInfo != null)
            {
                vPropInfo.SetValue(pInput, pVal);
            }

            return pInput;
        }

        /// <summary>验证数据集中的表是否存在行级记录
        /// </summary>
        public static bool HasRowRecord(this DataSet ds, string pTableName = null)
        {

            if (ds != null && ds.Tables.Count > 0)
            { 
                if(pTableName == null)
                {
                    return ds.Tables[0].Rows.Count > 0;
                }
                else
                {
                    return ds.Tables[pTableName].Rows.Count > 0;
                }
            }

            return false;
        }

    }
}
